import 'dart:core';
import 'dart:io';

import 'package:mybase/base/tool/empty/FFEmpty.dart';

///Platform.pathSeparator表示路径分隔符，对于Android和iOS来说表示‘/’
///recursive：false代表操作当前目录。true表示包含所有子目录

class FFile {
  //判断文件类型
  static bool isFile(String filePath) {
    FileSystemEntityType type = FileSystemEntity.typeSync(filePath);
    return type == FileSystemEntityType.file;
  }

  //判断文件是否存在
  static Future<bool> isExistFile(String filePath) async {
    if (!isFile(filePath)) return false;

    File file = File(filePath);
    return file.exists();
  }

  //创建文件
  static Future<File?> createFile(String filePath) async {
    File file = File(filePath);
    try {
      return await file.create(recursive: true);
    } catch (e) {
      print("createFile error = $e");
      return null;
    }
  }

  //删除文件
  static Future<bool> removeFile(String filePath) async {
    if (isFile(filePath)) {
      File file = File(filePath);
      try {
        await file.delete(recursive: true);
        return true;
      } catch (e) {
        print("removeFile error = $e");
        return false;
      }
    }
    return true;
  }

  //向文件写内容
  static Future<bool> writeContentByPath(
      {required String filePath,
      required String content,
      bool? autoCreate = false,
      FileMode mode = FileMode.append}) async {
    return await writeContentByFile(
        file: File(filePath),
        content: content,
        autoCreate: autoCreate,
        mode: mode);
  }

  static Future<bool> writeContentByFile(
      {required File file,
      required String content,
      bool? autoCreate = false,
      FileMode mode = FileMode.append}) async {
    try {
      if (autoCreate == true) {
        bool fileExists = await file.exists();
        if (fileExists == false) {
          await file.create(recursive: true);
        }
      }
      await file.writeAsString(
        content,
        mode: mode,
        flush: true,
      );
      return true;
    } catch (e) {
      print("writeContentByFile error = $e");
      return false;
    }
  }

  static Future<File?> copy(String filePath, String toFilePath) async {
    if (!isFile(filePath)) {
      return null;
    }
    ;

    File file = File(filePath);
    try {
      File toFile = await file.copy(toFilePath);
      return toFile;
    } catch (e) {
      print("FFile copy e = $e");
      return null;
    }
  }

  ///一次性读取文件
  static Future<String> readAsString(String filePath) async {
    if (!isFile(filePath)) return "";

    File file = File(filePath);
    String content = await file.readAsString();
    if (!FFEmpty.stringIsEmpty(content)) {
      return content;
    }
    return "";
  }

  ///一次读取，按需返回。注意性能问题
  ///startLine  开始行数，从0开始算
  ///endLine    结尾行数
  ///isTrim     去掉首尾空格
  static Future<List<String>?> readByLine(
    String filePath, {
    int? startLine = 0,
    int? endLine,
    bool? isTrim,
  }) async {
    if (!isFile(filePath)) return null;

    File file = File(filePath);
    List<String> lines = await file.readAsLines();

    if (FFEmpty.listIsEmpty(lines)) {
      return null;
    }

    if (startLine == null || startLine < 0) {
      startLine = 0;
    }

    if (startLine >= lines.length) {
      return null;
    }

    endLine ??= lines.length - 1;

    if (endLine >= lines.length) {
      endLine = lines.length - 1;
    }

    List<String> results = [];
    for (int i = startLine; i <= endLine; i++) {
      String line = lines[i];
      if (isTrim == true) {
        line = line.trim();
      }
      results.add(line);
    }

    return results;
  }
}
